//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

var baseURL = "../../";

//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;

//全局配置
$.ajaxSetup({
	dataType: "json",
	cache: false
});

//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    return grid.getGridParam("selarrrow");
}

//判断是否为空
function isBlank(value) {
    return !value || !/\S/.test(value)
}


function ajax(url,data,type,async,cache){
	var deferred = $.Deferred();
	$.ajax({
		url:url,
		type:type,
		timeout:60000,
		data:data,
		cache:cache,
		dataType:'json',
		async:async,
		contentType:'application/json',
		success:function(msg){
			deferred.resolve(msg)
		},
		error:function(error){
			deferred.reject(error);
		}
	});
	return deferred.promise()
}

function ajaxForm(url,data,type,async,cache){
	var deferred = $.Deferred();
	$.ajax({
		url:url,
		type:type,
		timeout:60000,
		data:data,
		cache:cache,
		dataType:'json',
		async:async,
		// contentType:'application/json',
		success:function(msg){
			deferred.resolve(msg)
		},
		error:function(error){
			deferred.reject(error);
		}
	});
	return deferred.promise()
}

function postForm(url,data){
	var deferred = $.Deferred();
	this.ajaxForm(url,data,'post',true,false).done(function(e){
		deferred.resolve(e);
	}).fail(function(e){
		deferred.reject(e);
	});
	return deferred.promise()
}

function post(url,data){
	var deferred = $.Deferred();
	this.ajax(url,data,'post',true,false).done(function(e){
		deferred.resolve(e);
	}).fail(function(e){
		deferred.reject(e);
	});
	return deferred.promise()
}

function get(url,data){
	var deferred = $.Deferred();
	this.ajax(url,data,'get',true,false).done(function(e){
		deferred.resolve(e);
	}).fail(function(e){
		deferred.reject(e);
	});
	return deferred.promise()
}


/**************************************时间格式化处理************************************/
function dateFtt(fmt,date)
{ //author: meizz
	var o = {
		"M+" : date.getMonth()+1,                 //月份
		"d+" : date.getDate(),                    //日
		"h+" : date.getHours(),                   //小时
		"m+" : date.getMinutes(),                 //分
		"s+" : date.getSeconds(),                 //秒
		"q+" : Math.floor((date.getMonth()+3)/3), //季度
		"S"  : date.getMilliseconds()             //毫秒
	};
	if(/(y+)/.test(fmt))
		fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("("+ k +")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
	return fmt;
}

//创建时间格式化显示
function crtTimeFtt(value,row,index){
	var crtTime = new Date(value);
	return top.dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);//直接调用公共JS里面的时间类处理的办法     
}



/**
 * 启动定时任务
 * @param id
 */
setIntervalInit=function (data) {
	var setInt = setInterval(
		function () {
			$.ajax({
				url: baseURL + '/export/checkFile',
				data: JSON.stringify(data),
				type: 'POST',
				dataType: 'JSON',
				global: false,//表示是否触发全局ajax事件。设置为false将不会触发全局ajax事件
				contentType:'application/json;charset=UTF-8',
				success: function (ex) {
					if (ex.code == 0) {
						window.location = baseURL + "/export/file?filePath=" + ex.filePath;
						clearInterval(setInt);
						vm.result = false;
						return;
					}
				}
			});
		},
		2000
	);
}



excel=function (url,data) {
	if(vm.result){
		layer.msg("正在生成下载文件，请稍后！(注：请勿关闭浏览器,在本系统进行其他操作");
		return;
	}
	vm.result = true;
	$.ajax({
		url: url,
		type: "post",
		data: JSON.stringify(data),
		processData: false,
		dataType: "json",
		contentType: false,
		success: function (r) {
			if (r.code == 0) {
				layer.msg("正在生成下载文件，请稍后！(注：请勿关闭浏览器,在本系统进行其他操作");
				setIntervalInit({"filePath":r.filePath})
			} else {
				vm.result = false;
				alert(r.msg);
			}
		}
	});
}


check = function (options,msg) {
	if(null == options || options.length== 0 ){
		alert(msg);
	}
}

